package com.zy.config;


import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Autowired
    private SaTokenConfig saTokenConfig;

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
//        registry.addInterceptor(new SaInterceptor(handle -> {
//                    StpUtil.checkLogin();
//                    log.info("当前账号=={}，token是==={}", StpUtil.getLoginId(), StpUtil.getTokenValue());
//                    log.info("更新前，当前登录有效时间：" + StpUtil.getTokenTimeout());
//                    StpUtil.renewTimeout(saTokenConfig.getTimeout()); // 更新过期时间
//                    log.info("更新后，当前登录有效时间：" + StpUtil.getTokenTimeout());
//
//                }))
//                .addPathPatterns("/**")
//
//                .excludePathPatterns("/api/v1/bus/auth/**", "/static/**","/doc/**","/test/**");

    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.useRegisteredExtensionsOnly(true)
                .favorParameter(true)
                .parameterName("mediaType")
                .defaultContentType(MediaType.APPLICATION_JSON) // 默认响应JSON 视图
                .mediaType("json", MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML)
                .mediaType("html", MediaType.TEXT_HTML);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
    }


}